package com.chat.datea.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.chat.datea.R;
import com.chat.datea.bean.PlaylistEntity;
import com.chat.datea.utils.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/21.
 */
public class MusicPlayFragment_GridView_Adapter extends BaseAdapter{
    private List<PlaylistEntity.ContentEntity> content;
    private PlaylistEntity playlist;
    private Context mContext;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    public MusicPlayFragment_GridView_Adapter(Context c, PlaylistEntity entity){
        mContext = c;
        playlist = entity;
        content = playlist.getContent();
        volleySingleton = VolleySingleton.getVolleySingleton(mContext);
        imageLoader = volleySingleton.getImageLoader();
    }


    @Override
    public int getCount() {
        return content != null && content.size() > 0 ? content.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return content != null && content.size() > 0 ? content.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            holder = new ViewHolder();
            //通过布局加载器加载布局文件
            convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_playlist_item, null);
            //初始化控件
            holder.playList_Image = (ImageView) convertView.findViewById(R.id.playList_Image);
            holder.playList_Describe = (TextView) convertView.findViewById(R.id.playList_Describe);
            holder.playList_Type = (TextView) convertView.findViewById(R.id.playList_Type);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        //设置组件
        holder.playList_Describe.setText(playlist.getContent().get(position).getTitle());
        holder.playList_Type.setText(playlist.getContent().get(position).getTag());
        String url = playlist.getContent().get(position).getPic_w300();
        ImageLoader.ImageListener imageListener = ImageLoader.getImageListener
                (holder.playList_Image, R.mipmap.default_recommend, R.mipmap.default_recommend);
        imageLoader.get(url, imageListener);


        return convertView;
    }

    public class ViewHolder{
        private ImageView playList_Image;
        private TextView playList_Describe, playList_Type;

    }
}
